<?php
class ReserveModuleFieldAction extends BaseAction {
	
	const TYPE = 'module';
	
	public function index() {
		
		$reserve_module_id = $this->_get("reserve_module_id");
		$reserve_module = ReserveModuleHelper::getObject("reserve_module_id={$reserve_module_id}");
		$list = ReserveModuleFieldHelper::getList("reserve_module_id={$reserve_module_id}","order_num DESC");
		
		$this->assign("list", $list);
		$this->assign("reserve_module_id", $reserve_module_id);
		$this->assign("reserve_module", $reserve_module);
		$this->assign("type", self::TYPE);
		$this->display();
	}
	
	//[操作流]默认页面{修改} Or {新增}
	//当传过来的ID有值，则进行修改操作流
	//当传过来的ID没值，则进行新增操作流
	public function todo(){
		$referUrl = $_SERVER['HTTP_REFERER'];
		$reserve_module_id = $this->_get("reserve_module_id");
		$id = $this->_get("id");
		$reserve_module = ReserveModuleHelper::getObject("reserve_module_id={$reserve_module_id}");
		$obj = ReserveModuleFieldHelper::getObject("reserve_module_field_id={$id}");
		if(!empty($obj)){
			$obj['validate_reg_cont'] = stripslashes($obj['validate_reg_cont']);
		}
		
		$this->assign("obj",$obj);
		$this->assign("referUrl",$referUrl);
		$this->assign("reserve_module_id", $reserve_module_id);
		$this->assign("reserve_module", $reserve_module);
		$this->assign("type", self::TYPE);
		$this->display();
	}
	
	public function add(){
		
		$data['form_type'] = $this->_post("form_type");
		$data['field_name'] = $this->_post("field_name");
		$data['field_desc'] = $this->_post('field_desc');
		$data['field_tip'] = $this->_post('field_tip');
		$data['validate_reg_cont'] = $this->_post('validate_reg_cont');
		$data['error_tip'] = $this->_post('error_tip');
		$data['reserve_module_id'] = $this->_post("reserve_module_id");
		
		$obj = ReserveModuleHelper::getObject("reserve_module_id={$data['reserve_module_id']}");
		if(empty($obj)){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return;
		}
		//表单名称验证
		if(empty($data['form_type'])){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "请选择字段类型！");
			return;
		}
		//检查field_name是否存在
		$list = ReserveModuleFieldHelper::getList("field_name='{$data['field_name']}' AND reserve_module_id={$data['reserve_module_id']}");
		if(!empty($list)){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "字段名填写的值已被占用，请重新输入！");
			return;
		}
		//只有FORM_TYPE为选项时，才有select_type和select_val
		if($data['form_type'] == FormType::$SELECT){
			$data['select_type'] = $this->_post("select_type");
			$data['select_val'] = $this->_post("select_val");
		}else{
			$data['select_type'] = "";
			$data['select_val'] = "";
		}
		//除[数字]、[编辑器]、[多图片上传]、[多文件上传]类型的，都有varchar_len
		if($data['form_type'] != FormType::$NUMBER && $data['form_type'] != FormType::$EDITOR && $data['form_type'] != FormType::$MULTI_IMAGE && $data['form_type'] != FormType::$MULTI_FILE){
			$data['varchar_len'] = $this->_post("varchar_len");
		}else{
			$data['varchar_len'] = "";
		}
		
		$result = ReserveModuleFieldHelper::add($data);
		if($result === false){	//失败，则提示错误信息，并返回上一个页面[记录了上一个页面，用户填写的信息]
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return ;
		}
		
		//....添加字段
		$tablename = C("DB_PREFIX").$obj["table_name"];
		$varchar_len = $this->_post("varchar_len");
		$columnType = CommonUtil::getColumnType($data['form_type'], $varchar_len,$data['field_desc']);
		$sql = "ALTER TABLE {$tablename} ADD {$data['field_name']} {$columnType}";
		
		ReserveModuleFieldHelper::updateSQL($sql);
		echo CommonUtil::ajaxReturn(Status::$REDIRECT,null, __APP__.'/center/ReserveModuleField/?reserve_module_id='.$data['reserve_module_id']);
		
	}
	
	public function update(){
		
		$id = $this->_post("id");
		$data['form_type'] = $this->_post("form_type");
		$data['field_name'] = $this->_post("field_name");
		$data['field_desc'] = $this->_post('field_desc');
		$data['field_tip'] = $this->_post('field_tip');
		$data['validate_reg_cont'] = $this->_post('validate_reg_cont');
		$data['error_tip'] = $this->_post('error_tip');
		$data['reserve_module_id'] = $this->_post("reserve_module_id");
		
		$obj = ReserveModuleFieldHelper::getObject("reserve_module_field_id={$id}");
		if(empty($obj)){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return;
		}
		if($obj["is_system"] == 1){
			echo CommonUtil::ajaxReturn(Status::$SUCCESS, "系统字段不能修改！");
			return ;
		}
		$reserveModule = ReserveModuleHelper::getObject("reserve_module_id={$data['reserve_module_id']}");
		if(empty($reserveModule)){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return;
		}
		//表单名称验证
		if(empty($data['form_type'])){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "请选择字段类型！");
			return;
		}
		//检查field_name是否存在
		if($data['field_name'] != $obj["field_name"]){
			$list = ReserveModuleFieldHelper::getList("field_name='{$data['field_name']}' AND reserve_module_id={$data['reserve_module_id']}");
			if(!empty($list)){
				echo CommonUtil::ajaxReturn(Status::$ERROR, "字段名填写的值已被占用，请重新输入！");
				return;
			}
		}
		//只有FORM_TYPE为选项时，才有select_type和select_val
		if($data['form_type'] == FormType::$SELECT){
			$data['select_type'] = $this->_post("select_type");
			$data['select_val'] = $this->_post("select_val");
		}else{
			$data['select_type'] = "";
			$data['select_val'] = "";
		}
		//除[数字]、[编辑器]、[多图片上传]、[多文件上传]类型的，都有varchar_len
		if($data['form_type'] != FormType::$NUMBER && $data['form_type'] != FormType::$EDITOR && $data['form_type'] != FormType::$MULTI_IMAGE && $data['form_type'] != FormType::$MULTI_FILE){
			$data['varchar_len'] = $this->_post("varchar_len");
		}else{
			$data['varchar_len'] = "";
		}
		
		$result = ReserveModuleFieldHelper::update($data,"reserve_module_field_id={$id}");
		if($result === false){	//失败，则提示错误信息，并返回上一个页面[记录了上一个页面，用户填写的信息]
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return ;
		}
		
		//....添加字段
		$tablename = C("DB_PREFIX").$reserveModule["table_name"];
		$varchar_len = $this->_post("varchar_len");
		$columnType = CommonUtil::getColumnType($data['form_type'], $varchar_len,$data['field_desc']);
		$sql = "ALTER TABLE {$tablename} CHANGE {$obj["field_name"]} {$data["field_name"]} {$columnType}";
		
		ReserveModuleFieldHelper::updateSQL($sql);
		echo CommonUtil::ajaxReturn(Status::$REDIRECT,"修改成功！", __APP__.'/center/ReserveModuleField/?reserve_module_id='.$data['reserve_module_id']);
		
	}
	
	public function delete() {
		
		$id = $this->_get("id");
		
		//该ID是否存在
		$obj = ReserveModuleFieldHelper::getObject("reserve_module_field_id={$id}");
		if(empty($obj)){
			echo CommonUtil::ajaxReturn(Status::$SUCCESS, "对象不存在，操作失败！");
			return ;
		}
		if($obj["is_system"] == 1){
			echo CommonUtil::ajaxReturn(Status::$SUCCESS, "系统字段不能删除！");
			return ;
		}
		//删除是否成功
		$result = ReserveModuleFieldHelper::delete("reserve_module_field_id={$id}");
		if($result === false){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "系统繁忙，请稍后重试！");
			return ;
		}
		
		//删除字段
		$tablename = C("DB_PREFIX").$obj["table_name"];
		$sql = "ALTER TABLE {$tablename} DROP COLUMN {$obj["field_name"]}";
		
		ReserveModuleFieldHelper::updateSQL($sql);
		
		echo CommonUtil::ajaxReturn(Status::$RELOAD_PAGE, "操作成功！");
		
	}
	
	//修改推荐值  操作
    public function change_order_num(){
        $id = $this->_post("id");
        $orderNum = $this->_post('order_num');
        $data["order_num"] = $orderNum;
        $result = ReserveModuleFieldHelper::update($data,"reserve_module_field_id={$id}");
        if($result === false){
            echo CommonUtil::ajaxReturn(Status::$ERROR,"服务器繁忙，请稍后重试！");
        }else{
            echo CommonUtil::ajaxReturn(Status::$SUCCESS,"修改成功！");
        }
    }
	
	
	
}